package dao.book.imp;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import pojo.Book;
import pojo.User;
import util.DBUtil;
import dao.book.BookDao;

public class BookDaoImp implements BookDao {

	@Override
	public List<Book> findAll() {
		String sql = "select id,name,path from book";
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<Book> list = new ArrayList<Book>();
		Connection con =null;
		Book book;
		try {
		    con = DBUtil.createConnection();
			pst=con.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()){
				String name = rs.getString("name");
				String id = rs.getString("id").trim();
				String path = rs.getString("path").trim();
				book = new Book(id, name, path);
				list.add(book);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con,pst,rs);
		}
		return list;
	}

	@Override
	public Book findOne(String id) {
		String sql = "select id,name,path from book";
		PreparedStatement pst = null;
		ResultSet rs = null;
		Book book = null;
		Connection con =null;
		try {
		    con = DBUtil.createConnection();
			pst=con.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()){
				if(id.equals(rs.getString("id"))){
					String name = rs.getString("name");
					String path = rs.getString("path");
					book =new Book(id,name,path);
					}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con,pst,rs);
		}
		return book;
	}


}
